package com.demo.oauth2jFinalShiroServer.oauth2;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import com.demo.oauth2jFinalShiroServer.cache.OAuth2EntityCache;

public class ResourceAccessRealm extends AuthorizingRealm {

	@Override
	public String getName() {
		return ResourceAccessRealm.class.getSimpleName();
	}

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		Object principal = token.getPrincipal();
		if (OAuth2EntityCache.USER_TOKEN_MAP.containsKey(principal)) {
			AuthenticationInfo authenticationInfo = new SimpleAccount(principal, OAuth2EntityCache.USER_TOKEN_MAP.get(principal), getName());
			return authenticationInfo;
		}
		return null;
	}

	@Override
	public Class<?> getAuthenticationTokenClass() {
		return ResourceAccessToken.class;
	}

	
}
